package com.CmJava.sqlSessionFactoryBuilder.sqlSessionFactory.configuration.mapperConfig.mapperRegister;

import com.CmJava.mapper.MyInfoMapper;
import com.CmJava.session.SqlSession;
import com.CmJava.sqlSessionFactoryBuilder.sqlSessionFactory.configuration.mapperConfig.mapperRegister.mapperProxy.MapperProxyFactory;

import java.util.HashMap;
import java.util.Map;

public class MapperRegistry {
    //源码中是个map，存放扫描到的mapper接口类
    private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();

    public Map<Class<?>, MapperProxyFactory<?>> getKnownMappers() {
        return knownMappers;
    }

    public <T> T getMapper(Class<MyInfoMapper> type, SqlSession sqlSession) {

        if (!knownMappers.containsKey(type)){
            throw new RuntimeException("该mapper接口未被注册");
        }
        MapperProxyFactory<?> mapperProxyFactory = knownMappers.get(type);

        return (T) mapperProxyFactory.newInstance(sqlSession);


    }
}
